
package com.craterhill.java.ems.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.AutoPopulatingList;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.craterhill.java.ems.common.beans.Classes;
import com.craterhill.java.ems.common.beans.Section;
import com.craterhill.java.ems.common.utils.Constants;
import com.craterhill.java.ems.service.ClassesService;
import com.craterhill.java.ems.service.SectionService;
import com.craterhill.java.ems.service.StaffService;

@Controller
public class ClassSectionManagementController {

	private final StaffService staffService;
	private final ClassesService classesService;
	private final SectionService sectionService;

	@Autowired
	public ClassSectionManagementController(final StaffService staffService, final ClassesService classesService, final SectionService sectionService) {
		this.staffService = staffService;
		this.classesService = classesService;
		this.sectionService = sectionService;
	}

	@RequestMapping(value = "/setupClassSectionManagementForm.ems", method = RequestMethod.GET)
	public String setupForm(final @RequestParam(required = false, defaultValue = "0") int classId, final ModelMap modelMap) {
		modelMap.addAttribute("classesList", classesService.getClasses());
		modelMap.addAttribute("staffList", staffService.getStaffList(Constants.TEACHER_ROLE_ID));

		if(classId != 0){
			modelMap.addAttribute("classes", classesService.getClass(classId));
		}
		return "manage_class_section";
	}

	@RequestMapping(value = "/saveClassSection.ems", method = RequestMethod.POST)
	public String savePosition(final @ModelAttribute Classes classes) {
		if(classes.getClassesId() == 0)
			classesService.saveClass(classes);
		else
			classesService.updateClass(classes);
		return "redirect:setupClassSectionManagementForm.ems";
	}

	@RequestMapping(value = "/deleteClassSection.ems", method = RequestMethod.GET)
	public String deletePosition(final @RequestParam int classId) {
		classesService.deleteClass(classId);
		return "redirect:setupClassSectionManagementForm.ems";
	}

	@RequestMapping(value = "/getAjaxSectionList.ems", method = RequestMethod.GET)
	public void getAjaxSectionList(final @RequestParam int classId, final HttpServletResponse httpServletResponse) {
//		List<Section> sectionList = sectionService.getSections(classId);
//
//		JSONArray sessionListJson = new JSONArray();
//		JSONObject sessionJsonObj = null;
//		for(Section section : sectionList){
//			sessionJsonObj = new JSONObject();
//			sessionJsonObj.put("sectionId", section.getSectionId());
//			sessionJsonObj.put("sectionName", section.getSectionName());
//			sessionListJson.add(sessionJsonObj);
//		}
//
//		try{
//			httpServletResponse.getWriter().write(sessionListJson.toString());
//		} catch(IOException e){
//			e.printStackTrace();
//		}
	}

	@ModelAttribute
	public Classes getClasses() {
		Classes classes = new Classes();
		classes.setSections(new AutoPopulatingList<Section>(Section.class));
		return classes;
	}
}
